package com.zhixin.fans.ks.ui.splash;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import org.eenie.common.base.WinMode;
import org.eenie.common.subscribes.ProgressSubscriber;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.ui.base.BaseDaggerActivity;
import com.zhixin.fans.ks.ui.main.MainActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


public class SplashActivity extends BaseDaggerActivity {

    public static final String ROUTE_PATH = "/main/splash";
    @BindView(R.id.img_splash)
    ImageView mImgSplash;
    @BindView(R.id.tv_splash_hint)
    TextView mTvSplashHint;
    @BindView(R.id.tv_splash_conut)
    TextView mTvSplashConut;

    @Override
    public int initContentView() {
        return R.layout.activity_splash;
    }

    @Override
    public void initInjector() {
        setWinMode(WinMode.IMMERSE);
        QMUIStatusBarHelper.setStatusBarLightMode(this);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        Glide.with(this)
                .asBitmap()
                .placeholder(new ColorDrawable(getResources().getColor(R.color.colorPrimary)))
                .load(R.mipmap.bg_splash_first)
                .transition(BitmapTransitionOptions.withCrossFade())
                .into(mImgSplash);


        mTvSplashConut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, MainActivity.class));

                finish();
            }
        });


        countDown(3).subscribe(new ProgressSubscriber<Long>() {
            @Override
            public void onNext(Long t) {
                mTvSplashConut.setText(String.valueOf(t));
            }

            @Override
            public void onFinish() {
                startActivity(new Intent(mContext, MainActivity.class));
                finish();
            }
        });

    }


    private Observable<Long> countDown(long time) {
        return Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(time + 1)
                .compose(bindToLifecycle())
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return time - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


}
